package client;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.TimeUnit;

public class MyClientHander extends ChannelInboundHandlerAdapter  {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("建立连接成功,开始发消息");
        //建立连接之后疯狂发消息,如果不发消息,服务端就会调用userEventTriggered超时接口
        for (int i = 0; i < 100; i++) {
            TimeUnit.SECONDS.sleep(3);
            byte[] bytes = "来自客户端的数据".getBytes();
            ByteBuf byteBuf = ctx.alloc().buffer().writeBytes(bytes);
            // 2.写数据
            ctx.channel().writeAndFlush(byteBuf);
        }

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("收到服务端消息:"+msg.toString());
    }
}
